// 项目首页
var express = require('express');
var router = express.Router();
const sql = require('../conf/sql')
const pool = require('../conf/db')
const variable = require('../utils/global_variable')
/* GET home page. */

//搜索接口
router.get('/search', function (req, res, next) {
  pool.query(sql.searchMovie, ['%' + req.query.mname + '%'], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
      i.mshowdate = i.mshowdate.getFullYear()
    }
    if (result.length == 0) {
      res.json({ code: 201, msg: '搜索失败' });
      return;
    } else {
      res.json({ code: 200, msg: '搜索成功', data: result });

    }
  })
});

//搜索框联想接口
router.get('/imagesearch', function (req, res, next) {
  const mname = req.query.mname
  pool.query('select mname, mimage, mshowdate, mcountry, mlanguage from movie where mname like ?',
  ['%' + req.query.mname + '%'], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
      i.mshowdate = i.mshowdate.getFullYear()
    }
    res.json({ code: 200, msg: '搜索成功', data: result });
    
  })
});

//请求轮播图
router.get('/carousel', function (req, res, next) {
  pool.query(sql.queryCarousel, (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result);
    if (result.length == 0) {
      res.json({ code: 201, msg: '轮播图请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '轮播图请求成功', data: result });

    }
  })
});

//请求首页电影
router.get('/movie', function (req, res, next) {
  //此时是10条数据
  pool.query(sql.queryIndexMovie, [10], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result.length);
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
      i.mshowdate = i.mshowdate.getFullYear()
    }
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

//请求首页电视剧
router.get('/teleplay', function (req, res, next) {
  //此时是10条数据
  pool.query(sql.queryIndexTeleplay, [10], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result.length);
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

//请求首页评分前十的电影
router.get('/score', function (req, res, next) {
  pool.query(sql.queryIndexTenMovie, (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    for (const i of result) {
      i.mimage = variable.prefix + i.mimage + variable.suffix  
    }
   
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

//点击电影更多按钮
router.get('/moremovie', function (req, res, next) {
  //此时是20条数据
  pool.query(sql.queryIndexMovie, [20], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result.length);
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

//点击电视剧更多按钮
router.get('/moreteleplay', function (req, res, next) {
  //此时是20条数据
  pool.query(sql.queryIndexTeleplay, [20], (err, result) => {
    if (err) {
      console.log(err);
      next(err)
      return;
    }
    console.log(result.length);
    if (result.length == 0) {
      res.json({ code: 201, msg: '请求失败' });
      return;
    } else {
      res.json({ code: 200, msg: '请求成功', data: result });

    }
  })
});

module.exports = router;